package com.hymjweb.demo.hymj.bill_code_manage.b_master.controller;

import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.hymj.bill_code_manage.b_master.bean.BMasterBillCodePojo;
import com.hymjweb.demo.hymj.bill_code_manage.b_master.service.BMasterBillCodeService;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.bill_code_manage.b_master.controller
 * @Author: luoge
 * @CreateTime: 2020-11-03 14:28
 * @Description:
 */

@Controller
@RequestMapping("/pages/bill_code_manage/b_master")
public class BMasterBillCodeController extends BaseController {

    @Autowired
    private BMasterBillCodeService bMasterBillCodeService;

    /**
     * 主单号创建初化
     *
     * @return
     */
    @RequestMapping("/createMasterBillCode")
    public String createbillnativecode() {
        XtUserPojo xtUserPojo = this.getLoginUser();
        setRequestAttribute("userId", xtUserPojo.getId());
        setRequestAttribute("userName", xtUserPojo.getName());
        setRequestAttribute("currentDate", DateUtil.getSysDateStr());
        return "pages/bill_code_manage/b_master/createMasterBillCode";
    }

    // masterBillCode

    /**
     * 主单查询初始化
     *
     * @return
     */
    @RequestMapping("/masterBillCode")
    public String masterBillCode() {
        setRequestAttribute("currentDate", DateUtil.getSysDateStr());
        return "pages/bill_code_manage/b_master/masterBillCode";
    }

    /**
     * 生成主单号
     * @param param
     * @return
     */
    @RequestMapping("/generateCode")
    @ResponseBody
    public Object generateCode(@RequestBody Map<String, Object> param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("主单号生成参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("主单号生成成功!");

        boolean flag = bMasterBillCodeService.generateCode(param);

        if (!flag) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("主单号生成失败!");
        }

        return baseResultMap;

    }

    /**
     * 显示单号
     * @param userId
     * @return
     */
    @RequestMapping("/showCode")
    @ResponseBody
    public Object showCode(@RequestParam(value = "userId", required = true) String userId) {
        return bMasterBillCodeService.showCode(userId);
    }

    /**
     * 保存主单列表
     * @param rows
     * @return
     */
    @RequestMapping("/saveCode")
    @ResponseBody
    public Object saveCode(@RequestBody List<BMasterBillCodePojo> rows) throws Exception {
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("主单号保存成功!");
        boolean result = bMasterBillCodeService.saveCode(rows);
        if(!result){
            baseResultMap.setOk(false);
            baseResultMap.setMessage("待保存的主单号已经在库中存在，不能重复保存!");
        }
        return baseResultMap;
    }

    @RequestMapping("/queryMasterBillCodeList")
    @ResponseBody
    public Object queryMasterBillCodeList(@RequestParam(required = false) Map param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("主单号查询参数错误!");
        }
        return bMasterBillCodeService.queryMasterBillCodeList(param);
    }

    /**
     * 根据 id 返回主单号对象
     * @param id
     * @return
     */
    @RequestMapping("/queryMasterBillCodeById")
    @ResponseBody
    public Object queryMasterBillCodeById(String id) throws Exception {
        if(DataTypeUtil.isEmptyStr(id)){
            throwException("主单号ID为空,获取主单号对象失败!");
        }

        BMasterBillCodePojo pojo = bMasterBillCodeService.fetch(id);

        if (pojo == null) {
            throwException("根据主单号【" + id + "】获取主单号对象失败!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setData(pojo);
        return baseResultMap;
    }


    /**
     * 调整主单号授权使用部门
     * @param param
     * @return
     */
    @RequestMapping("/adjustUseDepartment")
    @ResponseBody
    public Object adjustUseDepartment(@RequestBody Map<String, Object> param) throws Exception {
        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("主单号授权使用部门调整成功!");

        boolean flag = bMasterBillCodeService.adjustUseDepartment(param);

        if (!flag) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("主单号授权使用部门调整失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/deleteMasterBillCode")
    @ResponseBody
    public Object deleteMasterBillCode(@RequestBody(required = true) Map param) throws Exception {
        if (param == null || param.isEmpty()) {
            throwException("主单号删除参数错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("主单号删除成功!");

        //{deleteMasterBillCodes:canDeleteCodes}
        int affectRows = bMasterBillCodeService.deleteMasterBillCode(param);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("主单号删除失败!");
        }
        return baseResultMap;
    }

}
